package com.ericsson.epc.modules.ebmAutoCollector.job;

import org.apache.poi.util.Units;
import org.apache.poi.xddf.usermodel.chart.*;
import org.apache.poi.xwpf.usermodel.XWPFChart;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import java.io.*;
import java.util.List;

public class PoiWordTest {
    public static void main(String[] args) throws FileNotFoundException {

        final String templateurl="D://a.docx";
        final String returnurl="D://b.docx";
        InputStream is = new FileInputStream(new File(templateurl));
        XWPFDocument document;
        try {
            document = new XWPFDocument(is);
            List<XWPFParagraph> paragraphs  = document.getParagraphs();
            /*if(paragraphs !=null&& paragraphs.size()>0){
                for(XWPFParagraph paragraph : paragraphs){
                    List<XWPFRun> runs = paragraph.getRuns();
                    for (XWPFRun run : runs) {
                        //每次读取为一次输入（保存就算一次输入）
                        String text = run.getText(0);
                        System.out.println(run.getText(0));
                        run.setText("CCSCC图表测试",0);

                    }
                }

            }*/

            System.out.println("11111");


            XWPFChart chart = document.createChart(15 * Units.EMU_PER_CENTIMETER, 10 * Units.EMU_PER_CENTIMETER);

            //标题
            chart.setTitleText("CCSCC分析");
            //标题是否覆盖图表
            chart.setTitleOverlay(false);

            //图例位置
            XDDFChartLegend legend = chart.getOrAddLegend();
            legend.setPosition(LegendPosition.TOP_RIGHT);

            //CellRangeAddress(起始行号，终止行号， 起始列号，终止列号）
            //分类轴标数据，
//			XDDFDataSource<String> countries = XDDFDataSourcesFactory.fromStringCellRange(sheet, new CellRangeAddress(0, 0, 0, 6));
            XDDFCategoryDataSource countries = XDDFDataSourcesFactory.fromArray(new String[] {"CC0,SCC17","CC1,SCC18","CC1,SCC20","CC1,SCC21","CC1,SCC22"});
            //数据1，
//			XDDFNumericalDataSource<Double> values = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, 6));
            XDDFNumericalDataSource<Integer> values = XDDFDataSourcesFactory.fromArray(new Integer[] {17098242,9984670,9826675,9596961,8514877});
            //XDDFChartData data = chart.createData(ChartTypes.PIE3D, null, null);
            XDDFChartData data = chart.createData(ChartTypes.PIE, null, null);
            //设置为可变颜色
            data.setVaryColors(true);
            //图表加载数据
            data.addSeries(countries, values);


            //绘制
            chart.plot(data);



            //文件存在删除
            try {
                File file = new File(returnurl);
                if (file.exists()) {
                    file.delete();
                }
                FileOutputStream fos = new FileOutputStream(returnurl);
                document.write(fos);
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
